package actor_service

import (
	"shimmer/neusoft_club_backend/controllers/utils"
	"shimmer/neusoft_club_backend/dao/domain"
	"shimmer/neusoft_club_backend/services"
)

type FindActorQuery struct {
	ClubID    *int64                    `json:"club_id;omitempty" uri:"club_id" form:"club_id" query:"club_id" binding:"-"`
	ActorType *domain.AuthorizationRole `json:"actor_type;omitempty" xml:"actor_type" form:"actor_type" query:"actor_type" binding:"-" `
}

func FindAllActorWithUser(userID int64, page *utils.PaginationQuery) (res utils.Pagination, err error) {
	actors := make([]domain.UserActor, 0)
	db := services.DBEngine.Where("user_id=?", userID)
	res, err = utils.QueryAndPaginate(page, db, &actors)
	return
}

func FindAllActorWithUserAndClub(userID int64, clubID int64, page *utils.PaginationQuery) (res utils.Pagination, err error) {
	actors := make([]domain.UserActor, 0)
	db := services.DBEngine.Where("user_id=? and club_id =?", userID, clubID)
	res, err = utils.QueryAndPaginate(page, db, &actors)
	return
}

func FindActorWithQuery(userID int64, query *FindActorQuery, page *utils.PaginationQuery) (res utils.Pagination, err error) {
	actors := make([]domain.UserActor, 0)
	db := services.DBEngine.Where("user_id=?", userID).Joins(domain.UserActorFieldClub)
	if query.ActorType != nil {
		db = db.Where("role=?", *query.ActorType)
	}
	if query.ClubID != nil {
		db = db.Where("club_id=?", *query.ClubID)
	}
	res, err = utils.QueryAndPaginate(page, db, &actors)
	return
}
